package com.xintong.visualinspection.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xintong.visualinspection.bean.Menu;
import com.xintong.visualinspection.dao.master.MenuDao;
import com.xintong.visualinspection.service.BaseService;
import com.xintong.visualinspection.service.MenuService;

import lombok.Data;

/**
 * 文件名：UserServiceImpl 版本信息：日期：2017/3/30 Copyright 江苏省交通规划设计院 Corporation 2017
 * 版权所有.
 */
@Service
@Data
public class MenuServiceImpl extends BaseService implements MenuService {

	private static final org.slf4j.Logger logger = LoggerFactory.getLogger(MenuService.class);

	@Autowired
	private MenuDao menuDao;

	@Override
	public List<Menu> getMenus(Menu menu) {
		
		List<Menu> menus = 	menuDao.getMenu(menu);
		
		List<Menu> mlist = 	new ArrayList<>();
		
		Map<Integer,List<Menu>> map = new HashMap<>();
		//找出所有父
		for (Menu m : menus) {
			if(m.getParentId()!=null){
				List<Menu> menuList = map.get(m.getParentId());
				if(menuList==null){
					menuList = new ArrayList<>();
				}
				menuList.add(m);
				map.put(m.getParentId(), menuList);
			}
		}
		//找出子
		for (Integer mid : map.keySet()) {
			for (Menu m2 : menus) {
				if(m2.getId()==mid){
					m2.setChilds(map.get(mid));
					if(m2.getParentId()==null){
						mlist.add(m2);
					}
				}
			}
		}
		
		return mlist;
	}

	@Override
	public void insert(Menu menu) {
		menuDao.insert(menu);

	}

	@Override
	public void update(Menu menu) {
		menuDao.update(menu);
	}

	@Override
	public void delete(Integer id) {
		menuDao.delete(id);
	}

}
